package org.eclipse.acceleo.module.sample.service;

import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Property;

public class AssociationService {
	public static int getMultiplicity(Association association, Class ownerClass) {
		for (Property property : association.getMemberEnds()) {
			if (!property.getType().equals(ownerClass)) {
				return property.getUpper();
			}
		}
		throw new RuntimeException(
				"The association is not connected to the class.");
	}

	public static boolean isOneMany(Association association, Class ownerClass) {
		for (Property property : association.getMemberEnds()) {
			if (property.getType().equals(ownerClass)) {
				return (property.getUpper() == 1)
						&& (property.getOtherEnd().getUpper() != 1);
			}
		}
		throw new RuntimeException(
				"The association is not connected to the class.");
	}

	public static boolean isManyOne(Association association, Class ownerClass) {
		for (Property property : association.getMemberEnds()) {
			if (property.getType().equals(ownerClass)) {
				return (property.getUpper() != 1)
						&& (property.getOtherEnd().getUpper() == 1);
			}
		}
		throw new RuntimeException(
				"The association is not connected to the class.");
	}

	public static boolean isManyMany(Association association, Class ownerClass) {
		for (Property property : association.getMemberEnds()) {
			if (property.getType().equals(ownerClass)) {
				return (property.getUpper() != 1)
						&& (property.getOtherEnd().getUpper() != 1);
			}
		}
		throw new RuntimeException(
				"The association is not connected to the class.");
	}
	// public static int getMultiplicity(Property property) {
	// return property.getUpper();
	// }
}
